package com.blast.manager.adapter

import android.text.TextUtils
import android.view.View
import com.blast.manager.R
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.adapter.AbsKeyEventAdapter
import com.wxl.common.room.entity.LeiGuanDelayEntity
import com.wxl.common.util.StringUtils

class DetonatorAdapter : AbsKeyEventAdapter<LeiGuanDelayEntity> {

    val widths = ArrayList<Int>()

    constructor(data: List<LeiGuanDelayEntity>?) : super(R.layout.item_delay_list_layout,data)

    fun setWidths(widths:ArrayList<Int>){
        this.widths.clear()
        this.widths.addAll(widths)
    }

    override fun convert(helper: BaseViewHolder, item: LeiGuanDelayEntity) {
        super.convert(helper, item)
        if(this.widths.isNotEmpty()){
            setViewWidth(helper.getView(R.id.itemDelayNumberText)
                ,helper.getView(R.id.itemDelayCodeText)
                ,helper.getView(R.id.itemDelayTimeText)
                ,helper.getView(R.id.itemDelaySortText)
                ,helper.getView(R.id.itemDelayKongText)
                ,helper.getView(R.id.itemDelayCjqText))
        }
        helper.setText(R.id.itemDelayNumberText,(helper.adapterPosition+1).toString())
        helper.setText(R.id.itemDelayTimeText,item.delay)
        helper.setText(R.id.itemDelayCodeText, StringUtils.getUid(item.UID))
        if (!TextUtils.isEmpty(item.holeNumber)) {
            val hexString: String = StringUtils.HexToBin16(item.holeNumber)
            helper.setText(R.id.itemDelaySortText, StringUtils.getRowNumber(hexString).toString())
            helper.setText(R.id.itemDelayKongText, StringUtils.getHoleNumber(hexString).toString())
            helper.setText(R.id.itemDelayCjqText, item.collectorNumber)
        } else {
            helper.setText(R.id.itemDelaySortText, "")
            helper.setText(R.id.itemDelayKongText, "")
            helper.setText(R.id.itemDelayCjqText, "")
        }
    }
    private fun setViewWidth(vararg vs: View){
        (0 until vs.size).forEach { i ->
            vs[i].layoutParams.width = widths[i]
        }
    }
}